package com.woniuxy.credit_loans.collection_zhang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.credit_loans.collection_zhang.dto.PageDto;
import com.woniuxy.credit_loans.collection_zhang.service.BorrowService;
import com.woniuxy.credit_loans.collection_zhang.vo.BorrowEchartsVo;
import com.woniuxy.credit_loans.entity.Borrow;
import com.woniuxy.credit_loans.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Borrow")
public class BorrowController {

    @Autowired
    private BorrowService borrowService;

    @GetMapping("/Echarts")
    public  Result selectEcharts(){
         List<BorrowEchartsVo> listEcharts = borrowService.selectEcharts();
         return  Result.ok().data("Echarts",listEcharts);
    }
    @GetMapping("/TypeEcharts")
    public  Result selectTypeEcharts(){
        List<BorrowEchartsVo> TypeEcharts = borrowService.selectTypeEcharts();
        return  Result.ok().data("TypeEcharts",TypeEcharts);
    }

    @PostMapping("/pageSelect")
    public Result selectAll(@RequestBody PageDto pageDto){
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getPagesize(),true);
        List<Borrow>  list=borrowService.selectAll(pageDto.getInput());

        PageInfo<Borrow> pageInfo = new PageInfo<Borrow>(list);
        return Result.ok().data("list",list).data("pageInfo",pageInfo.getTotal());
    }

    @PostMapping()
    public  Result add(@RequestBody Borrow borrow){
         int i = borrowService.insertSelective(borrow);
        if (i==0) {
            return  Result.ok();
        }else {
            return  Result.error();
        }
    }

    @GetMapping("{id}")
    public Result selectById(@PathVariable Long id){
         Borrow borrow = borrowService.selectByPrimaryKey(id);
            return  Result.ok().data("borrow",borrow);
    }

    @PutMapping("")
    public  Result update(@RequestBody Borrow borrow){
         int i = borrowService.updateByPrimaryKeySelective(borrow);
        if (i==0) {
            return  Result.ok();
        }else {
            return  Result.error();
        }
    }

    @DeleteMapping("{id}")
    public  Result delete(@PathVariable Long id){
         int i = borrowService.deleteByPrimaryKey(id);
        if (i==0) {
            return  Result.ok();
        }else {
            return  Result.error();
        }
    }


}
